
import axios from 'axios'
import qs from 'qs'
import router from '../router/index'
axios.defaults.timeout = 15000 //设置请求时间
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8' //配置请求头信息
axios.defaults.headers.post['Accept'] = 'application/json' //配置请求头信息
//添加一个请求拦截器
axios.interceptors.request.use((config) => {
  if (config.data) {
    if (config.method == "post" && config.data.constructor !== FormData) config.data = qs.stringify(config.data)
  } else {
    if (config.method == "post") config.data = qs.stringify(config.data)
  }
  if (localStorage.getItem('token')) config.headers.token = localStorage.getItem('token')

  return config
}, function (err) {
  return Promise.reject(err)
})

//添加一个响应拦截器
axios.interceptors.response.use((response) => {
  if (response.data.code == 300) {
    localStorage.removeItem('token')
    window.location.href = window.location.host
  }
  return response.data
}, function (error) {
  // 对响应错误做点什么
  return Promise.reject(error)
})


export default axios;